<template>
	<view>
		<view v-for="(item,index) in listArray" :key="index"  class="agent-box">
			<image src="../../static/demo.jpg" mode="aspectFill"></image>
			<view>
				<view>
					<text>{{item.userName}}</text>
					{{item.phone}}
				</view>
				<view>
					<view>所在小区：{{item.address}}</view>
					<view>申请时间：2020-12-21</view>
				</view>
			</view>
			<view class="agent-btn">
				<view @click="fnPass(item.id)">通过</view>
				<view @click="fnRefuse(item.id)">拒绝</view>
			</view>
		</view>
	</view>
</template>

<script>
import store from '@/store/store.vue'
export default {	
	onLoad(){
		let _this = this
		uni.request({
			url: store.url + '/user/get_submit_application_agent?openId=' + getApp().globalData.open_id,
			method: 'GET',
			header: {
				"content-type": "application/json;charset=utf-8"						
			},
			success: (res) => {		
				console.log(res)
				if(res.data.status == 'success'){
					_this.listArray = res.data.message
				}else{
					uni.showModal({
						title: '提示',
						content: '错误: ' + res.data.message
					});	
				}															      
			},
			fail: (res)=>{
				uni.showModal({
					title: '提示',
					content: '访问服务器失败: ' + res.data.message
				});						
			}
		});		
	},
	data() {
		return {
			listArray: []
		};
	},
	methods: {
		fnPass(id) {
			uni.showModal({
				title: '提醒',
				content: '将该经纪人将与你绑定，确定通过?',
				success: res => {
					if (res.confirm) {
						uni.request({
						    url: store.url + '/user/pass_new_agent',
							method: 'POST',
							data:{
								userId: id
							},
						    header: {			
								"content-type": "application/json;charset=utf-8"
						    },
						    success: (res) => {	
								console.log(res)
								if(res.data.status == 'success'){
									if(res.data.message == 1){
										this.fnTurnPage(1)	
									}else{
										this.fnTurnPage(2)	
									}																							
								}else{
									uni.showModal({
									    title: '提示',
									    content: '错误: ' + res.data.message
									});	
								}								
						    },
							fail: (res)=>{
								uni.showModal({
								    title: '提示',
								    content: '访问服务器失败: ' + res.data.message
								});						
							}
						});												
					}
				}
			});
		},
		fnRefuse(id) {
			uni.showModal({
				title: '提醒',
				content: '确定拒绝该经纪人签约申请?',
				success: res => {
					if (res.confirm) {							
						uni.request({
						    url: store.url + '/user/reject_new_agent',
							method: 'POST',
							data:{
								userId: id,
							},
							header: {			
								"content-type": "application/json;charset=utf-8"
							},
						    success: (res) => {	

								if(res.data.status == 'success'){
									if(res.data.message == 1){
										this.fnTurnPage(2)
									}else{
										uni.showModal({
											title: '提示',
											content: '错误: 该用户已签约'
										});	
									}							
								}else{
									uni.showModal({
									    title: '提示',
									    content: '错误: ' + res.data.message
									});	
								}							
						    },
							fail: (res)=>{
								uni.showModal({
								    title: '提示',
								    content: '访问服务器失败: ' + res.data.message
								});						
							}
						});						
					}
				}
			});
		},
		fnTurnPage(type) {
			uni.navigateTo({
				url: '/pages/apply/state?param=' + encodeURIComponent(JSON.stringify(type))
			})
		}
	}
};
</script>

<style lang="scss" scoped>
.agent-box {
	display: flex;
	padding: 25px;
	border-bottom: 1rpx solid #ccc;
	box-sizing: border-box;
	background: #fff;

	image {
		margin-right: 20rpx;
		width: 136rpx;
		height: 136rpx;
		border-radius: 50%;
		overflow: hidden;
	}

	> view {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		flex: 1;
		overflow: hidden;

		> view {
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			line-height: 1.5;
			font-size: 24rpx;
			color: #bdbdbd;

			view {
				font-size: 24rpx;
			}

			text {
				margin-right: 20rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				font-size: 34rpx;
				font-weight: bold;
				color: #000;
			}
		}

		&.agent-btn {
			flex: inherit;
			align-items: center;

			view {
				width: 120rpx;
				border-radius: 10rpx;
				line-height: 66rpx;
				text-align: center;
				color: #fff;
				background: #f13b32;

				&:first-of-type {
					margin-bottom: 10rpx;
					background: #409eff;
				}
			}
		}
	}
}
</style>
